﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Entity.YiDongYuan;
using YiSha.Service.YiDongYuan;
using YiSha.Util;
using YiSha.Util.Extension;

namespace YiSha.Admin.WebApi.Controllers
{
    /// <summary>
    /// 厦门通创接口
    /// http://IP:PORT/Api/smokemeter/freeaccer/upload/addfadt
    /// </summary>
    [Route("Api/smokemeter/freeaccer/upload/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class XmtctController : Controller
    {
        /// <summary>
        /// 上传非道路自由加速
        /// </summary>
        /// <returns></returns>
        public async Task<string> addfadt([FromBody] object json)
        {
            LogHelper.Debug(json.ToString());
            var xmtctEntity = json.ToString().ToObject<Model.XmtctDataEntity>();
            DetectionResultEntity entity = new DetectionResultEntity()
            {
                VIN = xmtctEntity.tname,
                carbrand = xmtctEntity.tname,
                acclimits = xmtctEntity.limits.ParseToDouble(),
                Altitude = xmtctEntity.altitude,
                createTime = DateTime.Now,
                Hum = xmtctEntity.rh,
                k1 = xmtctEntity.kone.ParseToDouble(),
                k2 = xmtctEntity.ktwo.ParseToDouble(),
                k3 = xmtctEntity.kthree.ParseToDouble(),
                Latitude = xmtctEntity.latitude,
                Longitude = xmtctEntity.longitude,
                mechpower = xmtctEntity.rpower,
                Pres = xmtctEntity.ap,
                Rpm1 = xmtctEntity.rone,
                Rpm2 = xmtctEntity.rtwo,
                Rpm3 = xmtctEntity.rthree,
                DeviceID = xmtctEntity.did,
                testingtime = xmtctEntity.btime.ParseToDateTime(),
                testmethod = "0",
                testresult = xmtctEntity.prfares == 1 ? "0" : "1",
                Tmp = xmtctEntity.at,
                Data = xmtctEntity.pd.ToJson()
            };
            List<double> list = new List<double>() { entity.k1, entity.k2, entity.k3 };
            entity.accavg = list.Average();
            entity.accmax = list.Max();
            entity.accdiff = entity.accmax - entity.acclimits;
            await new DetectionResultService().SaveForm(entity);
            var obj = new
            {
                code = 200,
                message = "",
                data = "UPLOAD_SUCCESS"
            };
            return obj.ToJson();
        }
    }
}
